import java.util.Scanner;

public class Matrix {

	int[][] matrix = null;
	Scanner in = new Scanner(System.in);	
	
	// Constructors
	public Matrix(int n, int m)
	{
		matrix = new int[n][m];
		
		for (int i=0; i<n; i++)
			for (int j=0; j<m; j++)
			{
				System.out.print("A[" + i + "][" + j + "] = ");
				matrix[i][j] = in.nextInt();
			}		
	}
	
	public Matrix(int[][] mtx)
	{
		matrix = mtx.clone();
	}
	
	// Getter
	public int[][] getMatrix()
	{
		return matrix;
	}
	
	// Add
	public int[][] add(Matrix mtx)
	{
		int[][] adder = mtx.getMatrix();
		
		for (int i=0; i<matrix.length; i++)
			for (int j=0; j<matrix[i].length; j++)
				matrix[i][j] += adder[i][j];
		
		return matrix;
	}
	
	// Substract
	public int[][] substract(Matrix mtx)
	{
		int[][] adder = mtx.getMatrix();
		
		for (int i=0; i<matrix.length; i++)
			for (int j=0; j<matrix[i].length; j++)
				matrix[i][j] -= adder[i][j];
		
		return matrix;
	}
	
	// Multiply
	public int[][] multiply(Matrix mtx)
	{
		int[][] result = null;
		int[][] multiplier = mtx.getMatrix(); 
		
		// Generate the result[][]
		result = new int[matrix.length][];
		for (int i=0; i<matrix.length; i++)
			result[i] = new int[multiplier[i].length];
		
		// Multiply
		for (int i=0; i<matrix.length; i++)
			for (int j=0; j<multiplier[i].length; j++)
			{
				result[i][j] = 0;
				for (int k=0; k<matrix[i].length; k++)
					result[i][j] += matrix[i][k] * multiplier[k][j];
			}
		
		// Assign and return
		matrix = result;
		return matrix;
	}
	
	// Show
	public String toString()
	{
		StringBuffer str = new StringBuffer();
		
		for (int i=0; i<matrix.length; i++)
		{
			for (int j=0; j<matrix[i].length; j++)
				str.append(matrix[i][j] + " ");
			
			str.append("\n");
		}
		
		return str.toString();				
	}
	
	// Destructor
	protected void finalize() throws Throwable
	{
		in.close();
	}
}
